package com.sen.pro.switches.admin.content.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sen.common.struts.BaseAction;
import com.sen.common.util.Feedback;
import com.sen.common.util.web.ServletUtils;
import com.sen.pro.switches.core.cfg.ENV;
import com.sen.pro.switches.core.entity.News;
import com.sen.pro.switches.core.serivce.NewsService;
import com.sen.pro.switches.core.util.SiteFileUtil;

/**
 * 新闻管理Action
 * 
 * @author tian qingsen
 */
@Scope("prototype")
@Controller("newsManageAction")
@Namespace("/admin/content")
@InterceptorRefs(value = { @InterceptorRef(value = "switches-needsession-stack") })
public class NewsManageAction extends BaseAction {

	private static final long serialVersionUID = 6442489659959604402L;
	private static Logger LOG = Logger.getLogger(NewsManageAction.class);

	@Resource(name = "newsService")
	private NewsService newsService;

	private News news;
	private String htmlDesc;

	@Action(value = "news_manage", results = { @Result(name = SUCCESS, location = "/WEB-INF/view/admin/content/news_manage.jsp") })
	public String linksManage() {
		List<News> newsList = newsService.queryAllNews();
		request.setAttribute("newsList", newsList);
		return SUCCESS;
	}

	@Action(value = "news_edit", results = { @Result(name = SUCCESS, location = "/WEB-INF/view/admin/content/news_edit.jsp") })
	public String linksEdit() {
		if (news != null && news.getNewsId() != null) {
			news = newsService.getNewsById(news.getNewsId());
			String htmlName = news.getNewsId().intValue() + ".html";
			String htmlFile = ServletActionContext.getServletContext().getRealPath("/") + ENV.HTML_NEWS_DIR + "/"
					+ htmlName;
			try {
				htmlDesc = SiteFileUtil.getSiteHtmlFileToStr(htmlFile);
			} catch (IOException e) {
				LOG.error("读取新闻[" + news.getNewsTitle() + "]内容出错，原因：" + e.getMessage(), e);
			}
		}
		return SUCCESS;
	}

	@Action("news_doadd")
	public String newsDoAdd() {
		if (news == null) {
			writeJson(Feedback.fail("您填写的信息不完整！"));
			return NONE;
		}
		try {
			news.setFromIp(ServletUtils.getIpAddr(request));
			Integer insertId = newsService.insertNews(news);
			if (insertId != null && StringUtils.isNotBlank(htmlDesc)) {
				String htmlName = insertId.intValue() + ".html";
				// 保存新闻内容到html文件
				String htmlFile = ServletActionContext.getServletContext().getRealPath("/") + ENV.HTML_NEWS_DIR + "/"
						+ htmlName;
				SiteFileUtil.writeStrToSiteHtmlFile(htmlFile, htmlDesc);
			}
		} catch (Exception e) {
			LOG.error("新增新闻信息出错，原因：" + e.getMessage(), e);
			writeJson(Feedback.fail("操作异常，请稍后继续！"));
			return NONE;
		}
		writeJson(Feedback.success(""));
		return NONE;
	}

	@Action("news_dodelete")
	public String newsDoDelete() {
		if (news == null || news.getNewsId() == null) {
			writeJson(Feedback.fail("删除失败，新闻不存在！"));
			return NONE;
		}
		try {
			newsService.deleteNews(news.getNewsId());
			String htmlName = news.getNewsId().intValue() + ".html";
			// 保存新闻内容到html文件
			String htmlFile = ServletActionContext.getServletContext().getRealPath("/") + ENV.HTML_NEWS_DIR + "/"
					+ htmlName;
			SiteFileUtil.deleteSiteFile(htmlFile);
		} catch (Exception e) {
			LOG.error("删除新闻信息报错，原因：" + e.getMessage(), e);
			writeJson(Feedback.fail("操作异常，请稍后再试！"));
			return NONE;
		}
		writeJson(Feedback.success("删除成功！"));
		return NONE;
	}

	@Action("news_doedit")
	public String newsDoEdit() {
		newsService.updateNews(news);
		try {
			String htmlName = news.getNewsId().intValue() + ".html";
			// 保存新闻内容到html文件
			String htmlFile = ServletActionContext.getServletContext().getRealPath("/") + ENV.HTML_NEWS_DIR + "/"
					+ htmlName;
			SiteFileUtil.writeStrToSiteHtmlFile(htmlFile, htmlDesc);
		} catch (IOException e) {
			LOG.error("更新新闻信息出错，原因：" + e.getMessage(), e);
			writeJson(Feedback.fail("操作异常，请稍后再试！"));
			return NONE;
		}
		writeJson(Feedback.success(""));
		return NONE;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public String getHtmlDesc() {
		return htmlDesc;
	}

	public void setHtmlDesc(String htmlDesc) {
		this.htmlDesc = htmlDesc;
	}

}
